home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Style Layout ƒ / Style Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  13.8 KB  |  443 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Style Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "color library.c", "font library.c", "graphics debug library.c",
  11. |**|    "graphics library.c", "layout library.c", "shape library.c",
  12. |**|    "text library.c", and "transform library.c".
  13. |**|
  14. |**|    ©1992-1994  Apple Computer, Inc.
  15. |**|    All rights reserved.
  16. |**|
  17. |**| =====================================================================
  18. \**/
  19.  
  20.  
  21. #include "QDGX shell.h"
  22. #include "layout routines.h"
  23. #include "layout library.h"
  24.  
  25.  
  26. /**\
  27. |**| ---------------------------------------------------------------------
  28. |**| PROTOTYPES
  29. |**| ---------------------------------------------------------------------
  30. \**/
  31.  
  32. // funtions required by shell
  33.  
  34. void    DoSetup            (void);
  35. void    DoDraw            (WindowPtr wind, Boolean updating);
  36. OSErr    DoCreateNew        (void);
  37. void    DoDispose        (WindowPtr wind);
  38. void    DoIdle            (WindowPtr wind);
  39. void    DoTeardown        (void);
  40. void    DoClick            (WindowPtr wind, Point p);
  41.  
  42. // private functions
  43.  
  44. OSErr    DoWindowInit        (WindowPtr wind);
  45. void    CreateSampleImage    (WindowPtr wind);
  46.  
  47.  
  48. /**\
  49. |**| ---------------------------------------------------------------------
  50. |**| ENUMS
  51. |**| ---------------------------------------------------------------------
  52. \**/
  53. enum { rWindResource = 128 };
  54.  
  55.  
  56. /**\
  57. |**| ---------------------------------------------------------------------
  58. |**| GLOBALS
  59. |**| ---------------------------------------------------------------------
  60. \**/
  61. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  62. // functionality will only work with the "debugging" version of QuickDraw GX.
  63. // If the debugging version is not installed, nothing bad will happen, but these
  64. // functions will not work. 
  65.  
  66. Boolean        gDebugging = true;
  67.  
  68. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  69.  
  70. Boolean        gGiveMeValidation = true;
  71.  
  72.  
  73. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  74. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  75. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  76. // since we need abunch if we have several windows open.
  77.  
  78. long        gGraphicsHeapSize = 600;
  79.  
  80. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  81. // override.  This is so that our override can be native PowerPC code if necessary.
  82.  
  83. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  84.  
  85.  
  86.  
  87. /**\
  88. |**| ---------------------------------------------------------------------
  89. |**| DoSetup()
  90. |**| Here's where we initialize any global variables our application needs.
  91. |**| We have only one at this time -- the universal proc pointer for
  92. |**| our printing override.
  93. |**| ---------------------------------------------------------------------
  94. \**/
  95. void DoSetup (void)
  96. {    // Initialize our printing event override UPP
  97.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  98. }
  99.  
  100.  
  101. /**\
  102. |**| ---------------------------------------------------------------------
  103. |**| DoDraw()
  104. |**| Draw the contents of the window.  The first parameter is the window
  105. |**| to draw, and the second parameter is true if we're updating an existing
  106. |**| image.  If that's the case, we don't want to change anything, but
  107. |**| just draw what's already there.
  108. |**| ---------------------------------------------------------------------
  109. \**/
  110. void DoDraw (WindowPtr wind, Boolean updating)
  111. {
  112.      #pragma unused (updating)
  113.      GXDrawShape (GetDocShape(wind));
  114. }
  115.  
  116.  
  117. /**\
  118. |**| ---------------------------------------------------------------------
  119. |**| DoCreateNew()
  120. |**| This routine is called when a window needs to be created.
  121. |**| ---------------------------------------------------------------------
  122. \**/
  123. OSErr DoCreateNew (void)
  124. {
  125.     OSErr        err = noErr;
  126.     WindowPtr    wind;
  127.     
  128. // Get and create our window from the resource fork
  129.  
  130.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  131.  
  132. // Attach a default gxViewPort to it, create and iInitialize our
  133. // private data for it, and add a sample image to its page shape.
  134.  
  135.     if ( wind == NULL )
  136.         return (MemError());
  137.  
  138.     GXIgnoreGraphicsNotice(transform_already_set);
  139.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  140.     GXPopGraphicsNotice();
  141.     
  142.     err = DoWindowInit(wind);
  143.     if ( err != noErr )
  144.         return err;
  145.     
  146.     CreateSampleImage(wind);
  147.     return err;
  148. }
  149.  
  150.  
  151. /**\
  152. |**| ---------------------------------------------------------------------
  153. |**| DoDispose()
  154. |**| This routine is called when a window needs to be disposed of.
  155. |**| ---------------------------------------------------------------------
  156. \**/
  157. void DoDispose (WindowPtr wind)
  158. {
  159.     TH_Doc    doc;
  160.     
  161. // You should always dispose of your GX graphics objects before tossing your window.
  162. // Why?  It's generally good form and this approach guarantees that everything is
  163. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  164. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  165. // with notices set, you will receive a notice that you had not disposed of everything.
  166. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  167.     
  168.     if ( wind != NULL )
  169.     {
  170.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  171.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  172.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  173.         DisposHandle((Handle) doc);            // Dispose of our private data.
  174.         DisposeWindow(wind);                // Dispose of the window.
  175.     }
  176. }
  177.  
  178.  
  179. /**\
  180. |**| ---------------------------------------------------------------------
  181. |**| DoIdle()
  182. |**| This routine is called to do things while idling through the event loop.
  183. |**| ---------------------------------------------------------------------
  184. \**/
  185. void DoIdle (WindowPtr wind)
  186. {
  187. }
  188.  
  189.  
  190. /**\
  191. |**| ---------------------------------------------------------------------
  192. |**| DoTeardown()
  193. |**| This routine is called just before we quit to remove anything 
  194. |**| persistent that might have been setup by DoSetup().
  195. |**| ---------------------------------------------------------------------
  196. \**/
  197. void DoTeardown (void)
  198. {
  199.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  200. }
  201.  
  202.  
  203. /**\
  204. |**| ---------------------------------------------------------------------
  205. |**| DoClick()
  206. |**| ---------------------------------------------------------------------
  207. \**/
  208. void DoClick(WindowPtr window, Point p)
  209. {
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /**\
  217. |**| ---------------------------------------------------------------------
  218. |**| DoWindowInit()
  219. |**| In this function we create and initialize the the private document
  220. |**| structure for a new window.  This structure contains the print job and
  221. |**| the shape which is drawn in the window.  We store this data in a handle
  222. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  223. |**| this, rather than using globals, we can create many windows containing
  224. |**| unique print jobs and shapes.
  225. |**| ---------------------------------------------------------------------
  226. \**/
  227. OSErr DoWindowInit (WindowPtr wind)
  228. {
  229.     OSErr    err = noErr;
  230.     gxJob    docJob;
  231.     gxShape    docPage;
  232.     TH_Doc    windDoc;
  233.  
  234.  
  235. // Create the page shape. We set the unique items attribute to make sure that each item
  236. // added to the picture has a unique reference. If this attribute was not set, we would
  237. // not see all copies of anything we add to the shape multiple times -- we'd just see
  238. // the last version added.        
  239.  
  240.     docPage = GXNewShape(gxPictureType);
  241.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  242.     
  243.     
  244. // Create a print job for this document.  This will be the same as the system default until
  245. // the user goes through the dialogs for Page Setup or Print…
  246.  
  247.     err = GXNewJob(&docJob);
  248.     
  249.     
  250. // If there are no errors, create a handle the size of our document structure and store
  251. // the print job and page shape in it.  Store the handle in the window's refCon field so
  252. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  253. // can be used to do this easily.
  254.  
  255.     if ( err == noErr )
  256.     {
  257.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  258.  
  259.         if ( windDoc == NULL )
  260.             err = MemError();
  261.         else
  262.         {
  263.             (*windDoc)->docJob = docJob;
  264.             (*windDoc)->docPage = docPage;
  265.             SetWRefCon(wind, (long) windDoc);
  266.         }
  267.  
  268. // Now install our application override for PrintingEvent so that we can
  269. // support the new movable-modal printing dialog boxes.
  270.  
  271.         GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
  272.  
  273.     }
  274.  
  275.     return err;
  276. }
  277.  
  278.  
  279. /**\
  280. |**| ---------------------------------------------------------------------
  281. |**| CreateSampleImage()
  282. |**| This function creates primitive shapes and adds them to the window's page shape.
  283. |**| ---------------------------------------------------------------------
  284. \**/
  285. void CreateSampleImage (WindowPtr wind)
  286. {
  287.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  288.                                             // in QuickDraw coordinates
  289.     gxShape layout;                        // the layout shape we build
  290.     gxShape thePage;                    // this window's document shape
  291.     gxRunControls runControls;            // run controls for the layout shape
  292.     gxLayoutOptions layoutOptions;        // options for the layout shape
  293.     gxStyle timesStyle;                    // a style record for a Times-based style
  294.     gxStyle helveticaStyle;                // ditto for Helvetica
  295.     gxStyle baghdadStyle;                 // ditto for Baghdad
  296.     gxStyle textStyles[5];                // an array of text style for our text runs
  297.     
  298. // These are the five text runs for this layout shape, in pieces because they're
  299. // in different languages
  300.     
  301.     char *text1 = "Aetna ";
  302.     
  303. // The following is "Macintosh" in Arabic: 
  304. // meem, alif, kaf,  noon, tah,  wau,  shin
  305. // (This assumes the standard Arabic character set for Macintosh, by the way)
  306.     
  307.     static char text2[] = {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4, 0};
  308.     
  309.     char *text3 = " Office AWAY.";
  310.  
  311.     char *textRuns[3];                    // an array of pointers to text runs
  312.  
  313.     short textLengths[3];                // an array of the lengths of each run
  314.     short totalLength;                    // the total length of the layout's text
  315.     short level0 = 0;                    // a variable to take the address of later
  316.     
  317.     gxPoint posn;                        // the position of the layout shape
  318.  
  319.  
  320. // First, initialize the textRuns array to point to the five text runs
  321.     
  322.     textRuns[0] = text1;
  323.     textRuns[1] = text2;
  324.     textRuns[2] = text3;
  325.     
  326.     
  327. // Next, initialize the textLengths and levelRunLengths arrays.  MyStrLength() is
  328. // in this file.
  329.  
  330.     textLengths[0] = MyStrLength (text1);
  331.     textLengths[1] = MyStrLength (text2);
  332.     textLengths[2] = MyStrLength (text3);
  333.     
  334.     
  335. // totalLength is the length of all the text.
  336.  
  337.     totalLength = textLengths[0] + textLengths[1] + textLengths[2];
  338.     
  339.  
  340. // make default gxLayoutOptions and gxRunControls structures
  341.  
  342.     InitializeLayoutOptions (&layoutOptions);
  343.     InitializeRunControls (&runControls);
  344.     
  345.     
  346. // Position the layout half way down the left edge of the window. Set 
  347. // the layout's width to the window's width and set the flushness to 1/2, this
  348. // will cause the layout to center in the window.  Note that ourWindowRect is
  349. // not in fixed coordinates, so we have to make it fixed to use it.
  350.  
  351.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  352.     layoutOptions.flush = fract1/2;
  353.  
  354.     posn.x = 0;
  355.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  356.     
  357.  
  358. // Create the styles we'll use.  The helveticaStyle is 38-point Helvetica Italics.
  359. // NewLayoutStyle is a library routine found in layout library.c, and
  360. // SetStyleCommonFace found in text library.c.
  361.     
  362.     helveticaStyle = NewLayoutStyle(
  363.         (char *) "\pHelvetica",                // the gxFontName for the style
  364.         ff(38),                                // the text size in fixed point
  365.         0,                                    // gxTextAttributes
  366.         &runControls,                        // run controls (our default ones)
  367.         nil,                                // run features (none for this style)
  368.         0,                                    // count of run features
  369.         nil);                                // style run overrides (none right now)
  370.         
  371.     SetStyleCommonFace(helveticaStyle, italic);
  372.  
  373. // The second style is baghdadStyle -- Baghdad Plain (Arabic), 38 points.
  374.  
  375.     baghdadStyle = NewLayoutStyle(
  376.         (char *) "\pBaghdad Plain",            // gxFontName
  377.         ff(38),                                // text size (fixed point)
  378.         0,                                    // gxTextAttributes
  379.         &runControls,                        // run controls (our default ones)
  380.         nil,                                 // run features (none)
  381.         0,                                     // count of run features (none)
  382.         nil);                                // style run overrides (none)
  383.     
  384. // The final style is 38-point Times Roman with imposed right-to-left direction.
  385.  
  386.     runControls.flags |= gxImposeRightToLeft;
  387.     
  388.     timesStyle = NewLayoutStyle(
  389.         (char *) "\pTimes Roman",             // gxFontName
  390.         ff(38),                             // text size (fixed point)
  391.         0,                                    // gxTextAttributes
  392.         &runControls,                         // run controls (our altered ones)
  393.         nil,                                 // run features (none)
  394.         0,                                     // count of run features (none)
  395.         nil);                                // style run overrides (none)
  396.     
  397.  
  398. // Initialize the textStyles array so that each of the 3 runs has the right style
  399.  
  400.     textStyles[0] = helveticaStyle;
  401.     textStyles[1] = baghdadStyle;
  402.     textStyles[2] = timesStyle;
  403.     
  404.  
  405. // Setup complete!  Build the layout.
  406.  
  407.     layout = GXNewLayout(
  408.         3,                                // count of text runs
  409.         textLengths,                    // array of lengths of each run
  410.         (void *) textRuns,                // array of pointers to the text
  411.         3,                                // count of style runs
  412.         textLengths,                    // array of the byte lengths of each run
  413.         textStyles,                        // array of the styles
  414.         1,                                // number of levels in the layout
  415.         &totalLength,                    // array of the lengths of each level
  416.         &level0,                        // array of the levels
  417.         &layoutOptions,                    // options (default)
  418.         &posn);                            // position of the layout shape
  419.  
  420.     
  421. // Retrieve the page shape so we can add to it.
  422.  
  423.     thePage = GetDocShape(wind);
  424.     
  425. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  426.  
  427.     AddToShape(thePage, layout);
  428.  
  429. // Now that the shape is added to the picture, dispose of it and our styles
  430.  
  431.     GXDisposeShape(layout);
  432.     GXDisposeStyle(helveticaStyle);
  433.     GXDisposeStyle(baghdadStyle);
  434.     GXDisposeStyle(timesStyle);
  435.     
  436.  
  437. // Invalidate the window's portRect so that everything gets updated.
  438.     
  439.     SetPort(wind);
  440.     InvalRect(&ourWindowRect);
  441. }
  442.  
  443.